﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using ListaTelefonicaMEM.Classes;

namespace ListaTelefonicaMEM.Controles
{
    //event handlers
    public delegate void OnExpandirEventHandler(object sender, EventArgs e);
    public delegate void OnContrairEventHandler(object sender, EventArgs e);
    public delegate void OnAnteriorEventHandler(object sender, EventArgs e);
    public delegate void OnProximoEventHandler(object sender, EventArgs e);

    public class TreeViewPadraoLoadSegmentado : TreeView
    {
        //eventos
        public event OnExpandirEventHandler OnExpandir;
        public event OnContrairEventHandler OnContrair;
        public event OnAnteriorEventHandler OnAnterior;
        public event OnProximoEventHandler OnProximo;

        //propriedades
        private int FNivel = 0;
        private List<int> FNiveisCarregados = new List<int>();
        public int Nivel
        {
            get
            {
                return FNivel;
            }
            private set
            {
                if (FNivel > value)
                {
                    FNiveisCarregados.Remove(FNivel);
                }
                else
                {
                    FNiveisCarregados.Add(value);
                }
                FNivel = value;
            }
        }

        protected virtual void Expandir(EventArgs e)
        {
            if (OnExpandir != null)
            {
                Nivel++;
                OnExpandir(this, e);
            }
        }

        protected virtual void Contrair(EventArgs e)
        {
            if (OnContrair != null)
            {
                Nivel--;
                FNiveisCarregados.Remove(Nivel);
                OnContrair(this, e);
            }
        }

        protected virtual void Anterior(EventArgs e)
        {
            if (OnAnterior != null)
            {
                OnAnterior(this, e);
            }
        }

        protected virtual void Proximo(EventArgs e)
        {
            if (OnProximo != null)
            {
                OnProximo(this, e);
            }
        }



        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.Font = new Font(Config.NomeFonte, Config.TamanhoFonte);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);

            if (e.KeyData == Keys.Right)
            {
                Expandir(EventArgs.Empty);
                e.Handled = true;
            }
            if (e.KeyData == Keys.Left)
            {
                Contrair(EventArgs.Empty);
                e.Handled = true;
            }
            if (e.KeyData == Keys.Up)
            {
                Anterior(EventArgs.Empty);
                e.Handled = true;
            }
            if (e.KeyData == Keys.Down)
            {
                Proximo(EventArgs.Empty);
                e.Handled = true;
            }
        }

        //métodos
        public void CarregaNivel(string[] prStr)
        {
            if (Nivel == 0)
            {
                foreach (string item in prStr)
                {
                    Nodes.Add(item, item);
                }    
            }
            else
            {
                string wParent = SelectedNode.Text;

                foreach (string item in prStr)
                {
                    Nodes[wParent].Nodes.Add(item, item);
                }

                Nodes[wParent].Expand();
                SelectedNode = Nodes[wParent].Nodes[0];
                FNiveisCarregados.Add(Nivel);
            }
        }
    }
}
